<?php
class ProjectService extends Service {
    
    function __construct() {
        parent::__construct();
        $this->mDao = new ProjectDao();
    }

    public function insertObject($obj){
        try {
            $obj->setCreatedBy($this->userInformation());
            $status = new Status();
            $status->setId("in_progress");
            $status->IsLoaded(true);
            $obj->setStatus($status);
            
            $this->mDao->beginTrans();
            
                $Id = $this->mDao->insertObject($obj);
                
                if($Id==null) return null;
                    if($obj->getProjectAssignment()!=null){
                        $projectAssignmentDao = new ProjectAssignmentDao();
                        foreach($obj->getProjectAssignment() as $item){
                            $project = new Project();
                            $project->setId($Id);
                            $project->setIsLoaded(true);
                            $item->setProject($project);

                            if($projectAssignmentDao->insertObject($item)==null) return null;
                    }
                }

                if($obj->getTasks()!=null){
                    $taskDao = new TaskDao();
                    if($obj->getTasks() != null){
                        foreach($obj->getTasks() as $item){
                            $project = new Project();
                            $project->setId($Id);
                            $project->setIsLoaded(true);
                            $item->setProject($project);

                            $item->setCreatedBy($this->userInformation());

                            $status = new Status();
                            $status->setId("in_progress");
                            $status->setIsLoaded(true);
                            $item->setStatus($status);

                            if($taskDao->insertObject($item)==null) return null;
                        }
                    }
                }
            
            $this->mDao->commitTrans();
            return $Id;
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            $this->mDao->rollback();
        }
    }

    public function updateObject($obj,$oldId){
        try {
            $this->mDao->beginTrans();
            
                $status = new Status();
                $status->setId("IN PROGRESS");
                $status->IsLoaded(true);
                $obj->setStatus($status);
                
                $obj->setUpdatedBy($this->userInformation());
                if ($this->smartUpdate($obj,$oldId)==false) return null;

            $this->mDao->commitTrans();
            return true;
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            $this->mDao->roolback();
        }
    }

    public function getList() {
        try {
            return $this->mDao->getList();
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }
    
    public function getObject($id){
        try {
            return $this->mDao->getObject($id);
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }

//    public function deleteObject($id){
//        try {
//            if($this->mDao->deleteObject($id)==false) return false;
//            return true;
//        } catch (Exception $exc) {
//            PageData::addModelError($exc->getMessage());
//            $this->mDao->roolback();
//            throw new Exception($exc->getMessage());
//        }
//    }
    
    public function smartUpdate($obj,$oldId){
        if($this->mDao->updateObject($obj,$oldId)==false) return false;
        
        $projectAssignmentDao = new projectAssignmentDao();
        $projectAssignments = $projectAssignmentDao->getListByProjectId($obj->getId());
        
        if($obj->getProjectAssignment()!=null){
            foreach($obj->getProjectAssignment() as $item){
                $project = new Project();
                $project->setId($obj->getId());
                $project->setIsLoaded(true);
                $item->setProject($project);
                
                if(($item->getId() == null) || ($item->getId() == '')){
                    if($projectAssignmentDao->insertObject($item)==null) {
                        throw new Exception('Gagal menambahkan assignment project.');
                    }
                } else {
                    if($projectAssignmentDao->updateObject($item,$item->getId())==null) {
                        throw new Exception('Gagal mengupdate assignment project.');
                    }
                    if (($projectAssignments != null) && (count($projectAssignments) > 0)) {
                        $index_remove = -1;

                        foreach($projectAssignments as $index=>$projectAssignment) {
                            if ($projectAssignment->getId() == $item->getId()) {
                                $index_remove = $index;
                                break;
                            }
                        }
                        if ($index_remove > -1) {
                            unset($projectAssignments[$index_remove]);
                        }
                    }
                }
            }
           
            if (($projectAssignments != null) && (count($projectAssignments) > 0)) {
                foreach($projectAssignments as $item) {
                    if (!$projectAssignmentDao->deleteObject($item->getId())) {
                        throw new Exception('Gagal menghapus unused assignment project.');
                    }
                }
            }
        }
        
        $TaskDao = new TaskDao();
        $Tasks = $TaskDao->getListByProjectId($obj->getId());
        
        if($obj->getTasks()!=null){
            foreach($obj->getTasks() as $item){
                if(($item->getId() == null) || ($item->getId() == '')){
                    $project = new Project();
                    $project->setId($obj->getId());
                    $project->setIsLoaded(true);
                    $item->setProject($project);
                    
                    $item->setCreatedBy($this->userInformation());

                    $status = new Status();
                    $status->setId("OPEN");
                    $status->setIsLoaded(true);
                    $item->setStatus($status);
                    
                    if($TaskDao->insertObject($item)==null) {
                        throw new Exception('Gagal menambahkan assignment project.');
                    }
                } else {
                    $item->setLastUpdatedBy($this->userInformation());
                    
                    if($TaskDao->updateObject($item,$item->getId())==null) {
                        throw new Exception('Gagal mengupdate assignment project.');
                    }
                    if (($Tasks != null) && (count($Tasks) > 0)) {
                        $index_remove = -1;

                        foreach($Tasks as $index=>$Task) {
                            if ($Task->getId() == $item->getId()) {
                                $index_remove = $index;
                                break;
                            }
                        }
                        if ($index_remove > -1) {
                            unset($Tasks[$index_remove]);
                        }
                    }
                }
            }
           
            if (($projectAssignments != null) && (count($projectAssignments) > 0)) {
                foreach($projectAssignments as $item) {
                    if (!$projectAssignmentDao->deleteObject($item->getId())) {
                        throw new Exception('Gagal menghapus unused assignment project.');
                    }
                }
            }
        }
        
        return $obj;
        
    }
    
    
}
?>